"""
订单列表页面
"""
import allure
from selenium.common import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

from page.home_page import HomePage
from utils.logger import logger
from utils.utility_method import get_order_number, str_get_num


class OrderformPage(HomePage):
    _作废 = (By.XPATH, "//*[text()='作废']")
    _作废确认 = (By.CSS_SELECTOR, ".el-textarea__inner")
    _COMMIT = (By.CSS_SELECTOR, "[aria-label='单个作废'] .el-button--primary")
    _已作废 = (By.XPATH, "//div[text()='已作废']")
    _订单编号 = (By.CSS_SELECTOR, '[placeholder="请输入订单编号"]')
    _收件人 = (By.CSS_SELECTOR, '[placeholder="请输入收件人"]')
    _SEARCH = (By.XPATH, "//*[text()='查询']")
    # 复选框在已作废页面是第二个，在待发货是第一个
    _订单复选框 = (By.XPATH, "//*[@class='goods-body']/..//*[@class='el-checkbox__input']")
    _按选择删除确认 = (By.XPATH, "//*[text()='总共选择1条记录，此操作将永久删除所选订单']/../../..//*[@class='el-input__inner']")

    ele_list_index = {
        '待发货': 0,
        '待审核': 1,
        '预售': 2,
        '已发货': 3,
        '有售后-正常单': 4,
        '有售后-已推仓': 5,
        '已作废': 6,
        '所有订单': 7
    }

    id_list = {
        '所有订单': "pane-totalorder",
        '已作废': "pane-hascancel",
        '有售后': "pane-hasrefund",
        '已发货': "pane-finishdelivery",
        '预售': "pane-presale",
        '待审核': "pane-tocheck",
        '待发货': "pane-todelivery",
    }

    @allure.step("作废订单")
    def cancel_order(self):
        logger.info("作废订单")
        self.finds(*self._作废)[0].click()
        self.set_time(0.5)
        self.find_and_send("作废订单111", *self._作废确认)
        self.find_and_click(*self._COMMIT)
        return self

    @allure.step("前往已作废列表")
    def to_cancel_order_list(self):
        logger.info("前往已作废列表")
        self.find_and_click(*self._已作废)
        return self

    @allure.step("获取订单编号")
    def get_order_num(self):
        logger.info("获取订单编号")
        _编号 = (By.XPATH, "//*[@class='inline-block ml2']")
        num = self.finds(*_编号)[0].text
        return get_order_number(num)

    @allure.step("查询订单编号")
    def search_num(self, tab: str, order_number: str):
        logger.info("查询订单编号")
        self.finds(*self._订单编号)[self.ele_list_index.get(tab)].send_keys(order_number)
        # 查询按钮
        self.finds(*self._SEARCH)[self.ele_list_index.get(tab)].click()
        return self

    @allure.step("搜索收件人名称")
    def search_name(self, tab, name):
        logger.info("搜索收件人名称")
        self.finds(*self._收件人)[self.ele_list_index.get(tab)].send_keys(name)
        # 查询按钮
        self.finds(*self._SEARCH)[self.ele_list_index.get(tab)].click()
        self.set_time(1)
        r = f"//*[text()='收货信息：{name}']"
        try:
            self.find(By.XPATH, r)
            return True
        except NoSuchElementException:
            return False

    @allure.step('搜索手机号')
    def search_phone(self, tab, phone):
        logger.info('搜索手机号')
        _手机号 = (By.XPATH, f"//*[@id='{self.id_list.get(tab)}']//*[@placeholder='请输入手机号']")
        self.find_and_send(phone, *_手机号)
        # 查询按钮
        self.finds(*self._SEARCH)[self.ele_list_index.get(tab)].click()
        self.set_time(1)
        r = f"//*[text()='{phone}']"
        try:
            self.find(By.XPATH, r)
            return True
        except NoSuchElementException:
            return False

    def get_search_result(self, tab: str):
        self.set_time(1)
        result = (By.XPATH, f"//*[@id='{self.id_list.get(tab)}']//span[@class='el-pagination__total']")
        r = self.find(*result).text
        return str_get_num(r)

    @allure.step('修改订单金额')
    def update_money(self, tab, num):
        self.set_time(2)
        _修改订单金额 = (By.XPATH, f"//*[@id='{self.id_list.get(tab)}']//*[text()='修改订单金额']")
        ele = self.finds(*_修改订单金额)[0]
        action = ActionChains(self._driver)
        action.move_to_element(ele)
        action.perform()
        self.set_time(1)
        ele.click()
        _金额输入框 = (By.XPATH, f"//*[@id='{self.id_list.get(tab)}']//*[@placeholder='请输入订单金额']")
        self.find_and_send(num, *_金额输入框)
        _commit = (By.XPATH, '//*[@id="pane-0"]/div[35]/div/div[3]/div/div/button')
        self.find_and_click(*_commit)
        return self

    @allure.step('修改运费金额')
    def update_freight(self, tab, num):
        self.set_time(2)
        _修改订单金额 = (By.XPATH, f"//*[@id='{self.id_list.get(tab)}']//*[text()='修改订单金额']")
        ele = self.finds(*_修改订单金额)[0]
        action = ActionChains(self._driver)
        action.move_to_element(ele)
        action.perform()
        self.set_time(1)
        ele.click()
        _运费输入框 = (By.XPATH, f"//*[@id='{self.id_list.get(tab)}']//*[@placeholder='请输入订单运费']")
        self.find_and_send(num, *_运费输入框)
        _commit = (By.XPATH, '//*[@id="pane-0"]/div[35]/div/div[3]/div/div/button')
        self.find_and_click(*_commit)
        return self

    @allure.step('获取修改弹窗信息')
    def get_update_message(self):
        self.set_time(1)
        m = self._driver.execute_script('return document.querySelector(".el-message.el-message--success").textContent')
        return m

    def get_warning_message(self):
        self.set_time(1)
        m = self._driver.execute_script('return document.querySelector(".el-message.el-message--warning").textContent')
        return m

    @allure.step("勾选订单")
    def check_order(self, tab: str):
        logger.info("勾选订单")
        _订单单选框 = (By.XPATH, f"//*[@id='{self.id_list.get(tab)}']//*[@class='el-checkbox__input']")
        self.set_time(1)
        # 下标4对应的是第一个订单的单选框
        self.finds(*self._订单复选框)[4].click()
        return self

    @allure.step("删除订单")
    def delete_order(self, type: str):
        # todo 目前无法删除
        a = "'bottom-end'"
        按条件 = 'document.querySelectorAll("[x-placement=' + a + ']>li")[1].click()'
        按选择 = 'document.querySelectorAll("[x-placement=' + a + ']>li")[0].click()'
        ele = self.find(By.XPATH, "//*[text()='批量删除']")
        action = ActionChains(self._driver)
        action.move_to_element(ele)
        action.perform()
        self.set_time(1)
        if type == 'condition':
            self._driver.execute_script(按条件)
        elif type == 'choice':
            self._driver.execute_script(按选择)
        self.find_and_send('确认删除', *self._按选择删除确认)
        return self
